package com.flash.controller.admin;


import com.flash.dto.DishDTO;
import com.flash.dto.DishPageQueryDTO;
import com.flash.entity.Dish;
import com.flash.result.PageResult;
import com.flash.result.Result;
import com.flash.service.DishService;
import com.flash.utils.Output;
import com.flash.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品：{}",dishDTO);
        dishService.saveWithFlavor(dishDTO);

        /*每次新增菜品都清除缓存*/

        String key = "dish_"+dishDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult>page(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询:{}",dishPageQueryDTO);
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 菜品删除
     * @param ids
     * @return
     */
   @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        log.info("菜品删除：{}",ids);
        dishService.delete(ids);

        /*菜品删除后进行菜品更新*/
       cleanCache("dish_*");

        return Result.success();
    }

    /**
     * 通过Id查询菜品数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("通过Id查询菜品数据")
    public Result<DishVO> selectByIdWithFlavor(@PathVariable Long id){
        log.info("通过id查询菜品数据:{}",id);
        DishVO dishVO = dishService.selectByIdWithFlavor(id);
        return Result.success(dishVO);

    }

    /**
     * 更新菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("进行菜品数据更新操作")
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("进行菜品数据更新：{}",dishDTO);
        dishService.updateDishWithFlavor(dishDTO);
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     *菜品售卖状态修改
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("菜品售卖状态修改")
    @PostMapping("/status/{status}")
    public Result updateDishStatus(@PathVariable Integer status,Long id){
        log.info("菜品售卖状态修改:{},{}",status,id);
        dishService.updateStatus(status,id);
        return Result.success();
    }


    @GetMapping("/list")
    public Result<List<Dish>> getDishByCategoryId( Long categoryId){
        log.info("根据分类查询菜品数据；{}",categoryId);
        List<Dish> dishList = dishService.getDishByCategory(categoryId);
        Output.Output(dishList,"controller");
        return Result.success(dishList);
    }

    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
